package com.infore.airsupervise.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.adapter.RankDataListAdapter;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.AQIRealDataE;
import com.infore.airsupervise.bean.AirStationE;
import com.infore.airsupervise.biz.AQIDataRankBiz;
import com.infore.airsupervise.biz.impl.AQIDataRankBizImpl;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.HorizontalSelectScrollView;
import com.infore.airsupervise.ui.widget.SwipeRecyclerView;
import com.infore.airsupervise.utils.AirLevelUtil;
import com.infore.airsupervise.utils.JumpUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * 实时排名界面
 *
 * @作者: dingxinlin
 * @创建时间: 2017/3/19 15:51
 */
public class RealTimeSortActivity
        extends BaseTitleBarActivity
        implements SwipeRecyclerView.OnActionListener {

    @BindView(R.id.refreshRecyclerView)
    SwipeRecyclerView swipeRecyclerView;
    @BindView(R.id.sv_factor)
    HorizontalSelectScrollView svFactor;

    private AQIDataRankBiz realDataRankBiz;
    private boolean isRefresh;

    private int factorType = StatusConst.AIR_TYPE_AQI;     // 因子类型

    private List<AQIRealDataE.RankBean> datas = new ArrayList<>();
    private RankDataListAdapter quickAdapter = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_time_sort);
        ButterKnife.bind(this);

        realDataRankBiz = new AQIDataRankBizImpl();
        initView();
    }


    private void initView() {
        title.setText("实时排名");
        rightMenu.setVisibility(View.INVISIBLE);
        // 因子
        List<String> list_factor = Arrays.asList(getResources().getStringArray(R.array.airType));
        svFactor.setFactorList(list_factor);
        svFactor.setFactorChangerListener(factorChangerListener);

        // 适配器
        quickAdapter = new RankDataListAdapter(R.layout.item_station_range, datas , factorType);
        // 设置recyclerView
        swipeRecyclerView.setAdapter(quickAdapter);
        swipeRecyclerView.setActionListener(this);

        // 首次自动刷新
        swipeRecyclerView.getSwipeLayout().setRefreshing(true);
        swipeRecyclerView.onRefresh();
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        getRankData();
    }

    @Override
    public void onLoadMore() {
        isRefresh = false;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRecyclerView.getAdapter().loadMoreEnd();
                swipeRecyclerView.getSwipeLayout().setEnabled(true);
            }
        }, 1000);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        AQIRealDataE.RankBean infoE = datas.get(position);
        JumpUtil.jumpFromTo(ctx, NewAQIActivity.class, Constants.INTENT_STR_KEY, infoE.getMn());
    }

    /**
     * 发送网络请求 获取数据
     */
    private void getRankData() {
        Call<BaseResponse> call = realDataRankBiz.sendRealDataRankRequest(factorType, responseListener);
        requests.add(call);
    }

    private HorizontalSelectScrollView.FactorChangerListener factorChangerListener = new HorizontalSelectScrollView.FactorChangerListener() {
        @Override
        public void onChangeListener(String factor) {
            factorType = AirLevelUtil.getType(factor);
            quickAdapter.setFactorType(factorType);
            swipeRecyclerView.getSwipeLayout().setRefreshing(true);
            swipeRecyclerView.onRefresh();
        }
    };

    private OnServerResponseListener<List<AQIRealDataE.RankBean>> responseListener =  new OnServerResponseListener<List<AQIRealDataE.RankBean>>() {
        @Override
        public void onSucceed(List<AQIRealDataE.RankBean> result, String desc) {
            swipeRecyclerView.commonSuccessHandle(isRefresh, result.size());
            if(isRefresh){
                quickAdapter.getData().clear();
            }
            quickAdapter.addData(result);
            swipeRecyclerView.getRecyclerView().scrollToPosition(0);
        }

        @Override
        public void onFailed(int errorCode, String title, String desc) {
            swipeRecyclerView.commonFailedHandle(isRefresh, errorCode, title, desc);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}

